﻿#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

namespace Game.Editor
{
    /// <summary>
    /// 选择物体的窗口，选择的物体必须带tag，不然不予选中
    /// </summary>
    public class PickItemWindows : UnityEditor.EditorWindow
    {
        /// <summary>
        /// 设置目标manager
        /// </summary>
        Editor_ObjData m_editorDataManager;
        SceneObjGroupManager m_groupManager;
        /// <summary>
        /// 初始化
        /// </summary>
        ///<param name="opType">窗口将进行选取的类型</param>
        public void Init(Editor_ObjData _manager)
        {
            m_editorDataManager = _manager;
            m_groupManager = _manager.Editor_GetObjGroupManager();
            //m_currPickType = opType;
            //ref_arrGameObjectsTarget = pickToArrays;
            this.titleContent = new GUIContent("Select obj prefab");
            this.minSize = new Vector2(256, 200);

            SceneView.RepaintAll();
        }
        Vector2 scrollPos = new Vector2();
        int m_objectsCount;
        void OnGUI()
        {
            if (!m_editorDataManager)
                return;

            //--监听变动，这样才可以存储序列化信息到对应脚本中
            Undo.RecordObject(m_editorDataManager, "Set Data");
            GUILayout.Space(16);
            GUILayout.Label("Please select objects with tag ");
            GUILayout.Space(16);

            m_objectsCount = m_groupManager.GetObjSettingData().Length;

            //--显示大小
            m_objectsCount = EditorGUILayout.IntField("Game Objects Count:", m_objectsCount);

            //对应数目的取设约束
            if (m_objectsCount <= 0) m_objectsCount = 1;

            int currItemsCount = m_objectsCount;

            m_groupManager.ExpandGOArrayIfNeed( currItemsCount);

            //获得操作数组：
            var m_objSettingData = m_groupManager.GetObjSettingData();
            
            scrollPos = GUILayout.BeginScrollView(scrollPos);
            //Draw blocks one by one...
            for (int i = 0; i < currItemsCount; i++)
            {
                //Here we go:
                EditorGUILayout.BeginHorizontal();

                var currData = m_objSettingData[i];
                currData.prefabObj = (GameObject)EditorGUILayout.ObjectField(currData.prefabObj, typeof(GameObject), false);

                if (currData.prefabObj != null )
                {
                    if( string.IsNullOrEmpty( currData.prefabObj.tag) || currData.prefabObj.tag=="Untagged")
                    {
                        //Reject:
                        //新注释的Debug.LogWarning( "This objcet '" + arrStorage[i].name + "' is not marked as '" + targetTag + "' ." ); 
                        Debug.Log("Obj has no tag assigned, please set any tag for this type of objects.");
                        m_objSettingData[i].prefabObj = null;
                    }

                    //成功设置物体，则顺带设置所在index：
                    currData.prefabIndex = i;
                }
                //--校验结束--
                
                EditorGUILayout.EndHorizontal();
            }
            GUILayout.EndScrollView();

            if (GUILayout.Button("Finish", GUILayout.MinHeight(32)))
            {

                this.Close();
            }
        }

    }
}
#endif